package com.zx.club.oss.adapter;

import com.zx.club.oss.util.AliOssUtil;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 阿里云 OSS 存储服务
 * @author zx
 * @date 2025-03-20 09:19
 */

public class AliStorageServiceAdapter implements StorageServiceAdapter{

    private final AliOssUtil aliOssUtil;

    public AliStorageServiceAdapter(AliOssUtil aliOssUtil) {
        this.aliOssUtil = aliOssUtil;
    }

    @Override
    public String uploadFile(MultipartFile file) {
     try{
         //原始文件名
         String originalFilename = file.getOriginalFilename();
         //截取原始文件名的后缀   dfdfdf.png
         String fileName = originalFilename.substring(originalFilename.lastIndexOf("."));
         //构造新文件名称
         // String objectName = UUID.randomUUID().toString() + extension;

         //文件的请求路径
         return  aliOssUtil.upload(file.getBytes(), fileName);

     }catch (IOException e){
         log.error("文件上传失败,失败原因:{}",e.getMessage(), e);
        e.printStackTrace();
     }
        return null;
    }
}
